/*************************************************************************
	> File Name: w25qxx_dev.c
	> Author: life
	> Created Time: 2022年01月16日 星期日 20时40分31秒
 ************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/version.h>

static struct spi_master *spi_mas = NULL;
static struct spi_device * spi_dev =  NULL;

static struct spi_board_info w25qxx_info[] ={
	{
		.modalias = "w25qxx",  //匹配的唯一标识
		.max_speed_hz = 125000000, //DTS中发现应该是这个频率
		.bus_num = 0 , /*该SPI(从)设备所在总线的总线号,就记录了所属的spi_master之中的bus_num编号.一个spi_master就对应一条总线*/
		.mode    = SPI_MODE_0,
		.chip_select = 0,/*片选号.该SPI(从)设备在该条SPI总线上的设备号的唯一标识*/
	},
};
		
static int w25qxx_dev_init(void)
{
	int ret;
	spi_mas = spi_busnum_to_master(w25qxx_info[0].bus_num);
	if(spi_mas == NULL){
		printk("failed to find spi master \n");
		ret = PTR_ERR(spi_mas);
		return ret;
	}
	spi_dev = spi_new_device(spi_mas, w25qxx_info);
	if(spi_dev == NULL){
		printk("failed to create spi device \n");
		ret = PTR_ERR(spi_dev);
		goto exit_1;
	}
	
	return 0;
exit_1:
	if(spi_mas != NULL) 
		put_device(&spi_mas->dev);
	spi_mas = NULL;
	return ret;

}

static void w25qxx_dev_exit(void)
{
	if(spi_dev != NULL) 
		spi_unregister_device(spi_dev);
	if(spi_mas != NULL) 
		put_device(&spi_mas->dev);

}

module_init (w25qxx_dev_init);
module_exit (w25qxx_dev_exit);
MODULE_LICENSE("GPL");

